import { defineStore } from "pinia";
import { Message } from "@arco-design/web-vue";
import * as menuBaseApi from "@/api/menu-base";

export const useMenuBaseStore = defineStore("menuBase", () => {
  async function getMenus(param?: any) {
    const response = await menuBaseApi.getMenus(param);
    if (response.code === 20000) {
      return response.data.menus;
    }
    return [];
  }

  async function getMenu(param?: any) {
    const response = await menuBaseApi.getMenu(param);
    if (response.code === 20000) {
      return response.data.menu;
    }
    return {};
  }

  async function saveMenu(param?: any) {
    const apiCall = param.id ? menuBaseApi.updateMenu : menuBaseApi.createMenu;
    const response = await apiCall({ ...param });
    if (response.code === 20000) {
      Message.success(param.id ? "更新成功" : "新增成功");
    }
  }

  async function deleteMenu(param?: any) {
    const response = await menuBaseApi.deleteMenu(param);
    if (response.code === 20000) {
      Message.success("移除成功");
    }
  }

  return { getMenus, getMenu, saveMenu, deleteMenu };
});
